/*
	The MIT License

	Copyright (c) 2010 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

#include "ge_local.h"

/*-----------------------------------------------------------------------------
	Trimesh shapes :
-----------------------------------------------------------------------------*/

//
//	EGeometryFactory::CreatePlane
//
IPxTriMesh EGeometryFactory::CreatePlane( float size_x, float size_y, uint subdiv_x, uint subdiv_y )
{
	ETriMesh* mesh = new ETriMesh( GE_MESH_POSITION | GE_MESH_COLOR_0 | GE_MESH_COLOR_1 | GE_MESH_NORMAL | GE_MESH_UV_0 | GE_MESH_UV_1 );

	// Set num vertex
	mesh->SetVertexNum((subdiv_x+1)*(subdiv_y+1));

	float quad_x = size_x / subdiv_x;
	float quad_y = size_y / subdiv_y;
	float start_x = -size_x * 0.5f;
	float start_y = -size_y * 0.5f;

	// Set attributes
	uint index = 0;
	for ( uint i=0; i < subdiv_y+1; i++ )
	{
		for ( uint j=0; j < subdiv_x+1; j++)
		{
			EVertex vertex;
			vertex.color0 = vertex.color1 =  EColor::kWhite;

			vertex.position.x = start_x + j * quad_x;
			vertex.position.y = start_y + i * quad_y;
			vertex.position.z = 0;

			vertex.normal = EVector::kZ;

			// Set UV coord
			//if (i == 0 && j == 0)
			//	vertex.uv0 = vertex.uv1 = ETexCoord( 0.0f, 1.0f );
			//else if (i == 0 && j == subdiv_x)
			//	vertex.uv0 = vertex.uv1 = ETexCoord( 1.0f, 1.0f );
			//else if (i == subdiv_y && j == subdiv_x)
			//	vertex.uv0 = vertex.uv1 = ETexCoord( 1.0f, 0.0f );
			//else if (i == subdiv_y && j == 0 )
			//	vertex.uv0 = vertex.uv1 = ETexCoord( 0.0f, 0.0f );
			float	u	=	(float) j / (float) (subdiv_x);
			float	v	=	(float) i / (float) (subdiv_y);
			vertex.uv0	=	vertex.uv1	=	ETexCoord( u, v );

			mesh->SetVertex(index++, vertex);
		}
	}

	// Set index
	uint max_vertex_x = subdiv_x + 1;
	for ( uint i=0; i < subdiv_y; i++ )
	{
		for ( uint j=0; j < subdiv_x; j++)
		{
			mesh->AddTriangle(	j + max_vertex_x * i,			j + max_vertex_x * (i + 1),				(j + 1) + max_vertex_x * i ); 
			mesh->AddTriangle(	j + max_vertex_x * (i + 1),		(j + 1) + max_vertex_x * (i + 1),		(j + 1) + max_vertex_x * i ); 
		}
	}

	return mesh;
}


//
//	EGeometryFactory::CreateCube
//
IPxTriMesh EGeometryFactory::CreateCube( float size_x, float size_y, float size_z )
{
	SIGNAL("implement!");

	return NULL;
}


//
//	EGeometryFactory::CreateCylinder
//
IPxTriMesh EGeometryFactory::CreateCylinder( float height, float radius, uint subdiv_axis )
{
	return NULL;
}


//
//	EGeometryFactory::CreateSphere
//
IPxTriMesh EGeometryFactory::CreateSphere( float radius, uint subdiv_axis, uint subdiv_height )
{
	IPxTriMesh	mesh	=	ge()->CreateTriMesh( GE_MESH_POSITION | GE_MESH_NORMAL );
	int vert_num = 2 + subdiv_axis * subdiv_height;
	mesh->SetVertexNum(vert_num);
	
	EVertex v;
	v.position = EPoint(0, 0, -radius);
	v.normal = EVector(0, 0, -1);
	mesh->SetVertex(0, v);
	v.position = EPoint(0, 0, radius);
	v.normal = EVector(0, 0, 1);
	mesh->SetVertex(1, v);
	
	for (int i = 0; i < subdiv_axis; ++i)
	{
		float phi = 2*PI / subdiv_axis * i;
		EPoint dir(cos(phi), sin(phi));
		int base_idx = 2 + i * subdiv_height;
		int next_idx = 2 + ((i+1)%subdiv_axis) * subdiv_height;
		for (int j = 0; j < subdiv_height; ++j)
		{
			float theta = -0.5*PI + PI/(subdiv_height+1) * (j+1);
			EPoint p = dir * cos(theta);
			p.z = sin(theta);
			v.position = p * radius;
			v.normal = EVector(p);
			mesh->SetVertex(base_idx + j, v);
			if (j < subdiv_height - 1)
			{
				mesh->AddTriangle(base_idx + j, next_idx + j, next_idx + j + 1);
				mesh->AddTriangle(base_idx + j, next_idx + j + 1, base_idx + j + 1);
			}
		}
		mesh->AddTriangle( 0, next_idx, base_idx );
		mesh->AddTriangle( 1, base_idx+subdiv_height-1, next_idx+subdiv_height-1 );
	}
	
	return mesh;
}


//
//	EGeometryFactory::CreateCubeFromBBox
//	DHG : i'm not sure that it work properly.
//
IPxTriMesh	EGeometryFactory::CreateCubeFromBBox( const EBBox &bbox )
{
	IPxTriMesh	mesh	=	ge()->CreateTriMesh( GE_MESH_POSITION );
	mesh->SetVertexNum(8);
	
	EBBox bbox2	=	bbox;
	
	float	x0	=	bbox2.Min().x;
	float	y0	=	bbox2.Min().y;
	float	z0	=	bbox2.Min().z;
	float	x1	=	bbox2.Max().x;
	float	y1	=	bbox2.Max().y;
	float	z1	=	bbox2.Max().z;
	
	EVertex	v[8];
	v[0].position	=	EPoint( x0, y0, z0 );
	v[1].position	=	EPoint( x1, y0, z0 );
	v[2].position	=	EPoint( x0, y1, z0 );
	v[3].position	=	EPoint( x1, y1, z0 );
	v[4].position	=	EPoint( x0, y0, z1 );
	v[5].position	=	EPoint( x1, y0, z1 );
	v[6].position	=	EPoint( x0, y1, z1 );
	v[7].position	=	EPoint( x1, y1, z1 );

	// faces
	// 0 1 5 4
	// 1 3 7 5
	// 4 5 7 6
	// 0 2 3 1
	// 0 4 6 2
	// 2 6 7 3
	
	mesh->SetTriangleNum(12);
	
	mesh->SetTriangle( 0, 0, 1, 5 );
	mesh->SetTriangle( 1, 0, 5, 4 );
							  	  
	mesh->SetTriangle( 2, 1, 3, 7 );
	mesh->SetTriangle( 3, 1, 7, 5 );
							  	  
	mesh->SetTriangle( 4, 4, 5, 7 );
	mesh->SetTriangle( 5, 4, 7, 6 );
							  	  
	mesh->SetTriangle( 6, 0, 2, 3 );
	mesh->SetTriangle( 7, 0, 3, 1 );
							  	  
	mesh->SetTriangle( 8, 0, 4, 6 );
	mesh->SetTriangle( 9, 0, 6, 2 );
							  	  
	mesh->SetTriangle(10, 2, 6, 7 );
	mesh->SetTriangle(11, 2, 7, 3 );

	for (uint i=0; i<8; i++) {
		mesh->SetVertex( i, v[i] );
	}
	
	return mesh;
}
